/*
  ! 前途似海，来日方长。
  ! Created: 2024/12/26 09:31:23
*/
#include <bits/stdc++.h>
using namespace std;

#define x first
#define y second
// #define int long long
#define mp(x, y) make_pair(x, y)
#define eb(...) emplace_back(__VA_ARGS__)
#define fro(i, x, y) for (int i = (x); i <= (y); i++)
#define pre(i, x, y) for (int i = (x); i >= (y); i--)
inline void JYFILE19();

using i64 = long long;
using pii = pair<int, int>;

bool ST;
const int N = 2e5 + 10;
const int B = 600;
const int mod = 998244353;

int n, q, c, ct, ns, a[N], f[N];
int vs[N], hd[N], sz[N], cl[B][1 << 15];
struct edge { int to, nxt; } e[N << 1];

char buf[1<<21], *p1, *p2;
#define gc() (p1==p2&&(p2=(p1=buf)+cin.rdbuf()->sgetn(buf,1<<21),p1==p2)?EOF:*p1++)
template<typename T> inline void read(T &x) {
  x = 0; int q = 1; char z;
  while(!isdigit(z = gc())) if(z == '-') q = -1;
  while(isdigit(z)) x = x * 10 + (z ^ 48), z = gc(); x *= q;
}
inline void dfs(int x) {
  sz[x] = 1;
  for (int i = hd[x]; i; i = e[i].nxt) {
    if (e[i].to == f[x]) continue;
    dfs(e[i].to);
    sz[x] += sz[e[i].to];
  }
  if (sz[x] > B) {
    vs[x] = ++ct, sz[x] = 0;
  }
}
inline void sol(int x, int f, int c, int *cl) {
  c |= (1 << a[x]), cl[c]++;
  for (int i = hd[x]; i; i = e[i].nxt) {
    if (e[i].to == f) continue;
    sol(e[i].to, x, c, cl);
  }
}
inline bool sol(int x, int f, int c) {
  ns++;
  if (vs[x]) {
    ns = cl[vs[x]][c];
    return 1;
  }
  for (int i = hd[x]; i; i = e[i].nxt) {
    if (e[i].to == f) continue;
    if (c & (1 << a[e[i].to])) {
      if (sol(e[i].to, x, c)) return 1;
    }
  }
  return 0;
}

signed main() {
  JYFILE19();
  read(n), read(q), read(c);
  fro(i, 1, n) read(a[i]);
  fro(i, 2, n) read(f[i]);
  fro(i, 2, n) {
    e[++ct] = {f[i], hd[i]}, hd[i] = ct;
    e[++ct] = {i, hd[f[i]]}, hd[f[i]] = ct;
  }
  ct = 0;
  dfs(1);
  fro(i, 1, n) {
    if (vs[i]) {
      int *g = cl[vs[i]];
      sol(i, 0, 0, g);
      fro(j, 0, c - 1) {
        fro(k, 0, (1 << c) - 1) {
          if (k & (1 << j)) {
            g[k] += g[k ^ (1 << j)];
          }
        }
      }
    }
  }
  fro(i, 1, q) {
    int s, u;
    read(s), read(u);
    if (s & (1 << a[u])) {
      ns = 0;
      sol(u, 0, s);
      cout << ns << "\n";
    } else {
      cout << 1 << "\n";
    }
  }
  return 0;
}

bool ED;
inline void JYFILE19() {
  freopen("tree.in", "r", stdin);
  freopen("tree.out", "w", stdout);
  srand(random_device{}());
  ios::sync_with_stdio(0), cin.tie(0);
  double MIB = fabs((&ED - &ST) / 1048576.), LIM = 512;
  cerr << "MEMORY: " << MIB << endl, assert(MIB <= LIM);
}